<template>
  <div class="app-container">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="题目内容">
        <el-input
          clearable
          @clear="
            searchMap.where.content = '';
            search();
          "
          v-model="tempContent"
          placeholder="题目内容"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          @click="
            searchMap.where.content = tempContent;
            searchMap.now = 1;
            search();
          "
          >查询</el-button
        >
        <el-button
          type="primary"
          @click="quadd()"
          >添加题目</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      :data="quList"
      style="width: 100%"
      
    >
      <el-table-column type="selection" width="55"></el-table-column>
     
      <el-table-column prop="quType" label="题目类型"> 
         <template slot-scope="scope">
          <div>{{ scope.row.quType | quTypeFilter }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="level" label="难度等级">
        <template slot-scope="scope">
          <div>{{ scope.row.level | levelFilter }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="content" label="题目内容" :show-overflow-tooltip="true"> </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="150"> </el-table-column>
      <el-table-column prop="remark" label="题目备注" :show-overflow-tooltip="true"> </el-table-column>
      <el-table-column prop="analysis" label="整题解析" :show-overflow-tooltip="true"> </el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope"> 
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="
              form = scope.row;
              dialogFormVisible = true;
            "
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="warning"
            icon="el-icon-delete"
            @click="deleteById(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页栏 -->


    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="searchMap.now"
      :page-sizes="[5, 10, 30, 50]"
      :page-size="searchMap.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>


  </div>
</template>
<script>
import { fetchData,deleteId} from "@/api/qu";
export default {
  data() {
    return {
      tempContent:"",
      searchMap: {
        now: 1,
        size: 5,
        where: {
          content: "",
        },
      },
      quList: [],
      total: 0,
      deleteIds: [],
    };
  },
  methods:{
    quadd(){
      this.$router.push({
                path: "/qu/add",
                query: {},
              });
    },
    search() {
      fetchData(this.searchMap).then((res) => {
        this.quList = res.list;
        this.total = res.total;
      });
    },
    handleSizeChange(size) {
      this.searchMap.size = size;
      this.searchMap.now = 1;
      this.search();
    },
    handleCurrentChange(page) {
      this.searchMap.now = page;
      this.search();
    },
    deleteById(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteId(id).then((res) => {
            this.$message({
              type: res.code == 0 ? "success" : "warning",
              message: res.msg,
            });
            this.searchMap.now = 1;
            this.search();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  filters:{
    levelFilter(source){
      if (source == 1) {
        return '普通';
      }else if(source == 2){
        return '较难';
      }
    },
    quTypeFilter(source){
      var levelArr = [{value:1,label:"单选"},{value:2,label:"多选"},{value:3,label:"判断"},{value:4,label:"简答"}];
      var format = "";
      levelArr.forEach(item=>{
        if (item.value == source) {
          format = item.label;
        }
      })
      return format || "未知";
    }
    
  },
  created() {
    // 获取路由参数
    // console.log(this.$route.query.id);
    this.search();
  },
};
</script>